/*工具类*/
var Coreutil = (function () {
    var coreUtil = {};

    /*GET*/
    coreUtilsendGet = function (url, params, ft) {
        sendAJAX(url, params, ft, "GET")
    }

    /*POST*/
    coreUtilsendPost = function (url, params, ft) {
        sendAJAX(url, JSON.stringify(params), ft, "POST")
    }
    /*PUT*/
    coreUtilsendPut = function (url, params, ft) {
        sendAJAX(url, JSON.stringify(params), ft, "PUT")
    }
    /*DELETE*/
    coreUtilsendDelete = function (url, params, ft) {
        console.log("1  " + params);
        sendAJAX(url, JSON.stringify(params), ft, "DELETE")
    }


    /*ajax*/
    let sendAJAX = function (url, params, ft, method) {
        var loadIndex = top.layer.load(0, {shade: false});
        $.ajax({
            url: url,
            cache: false,
            async: true,
            data: params,
            type: method,
            contentType: 'application/json; charset=UTF-8',
            dataType: "json",
            beforeSend: function (request) {
                // request.setRequestHeader("authorization", Coreutil.getData("access_token"));
            },
            success: function (res) {
                top.layer.close(loadIndex);


                //  //关闭所有弹窗
                $(":input").val('');// 清空所有input输入框

                console.log(res);
                console.log("==================================" + res);
                if (res.code == 0) {
                    if (ft != null && ft != undefined) {
                        ft(res);
                    }
                    layer.closeAll();  //先关闭窗口在提示
                    layer.msg(res.msg, {icon: 6, time: 2000}, function () {

                    })


                } else if (res.code == 401001) { //凭证过期重新登录
                    layer.msg("凭证过期请重新登录", {time: 2000}, function () {
                        top.window.location.href = "/index/login"
                    })
                } else if (res.code == 401008) { //凭证过期重新登录
                    layer.msg("抱歉！您暂无权限", {time: 2000})
                } else {
                    layer.msg(res.msg);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                top.layer.close(loadIndex);
                if (XMLHttpRequest.status == 404) {
                    top.window.location.href = "/index/404";
                } else {
                    layer.msg("服务器好像除了点问题！请稍后试试");
                }
            }
        })
    }


    /*存入本地缓存*/
    coreUtil.setData = function (key, value) {
        layui.data('LocalData', {
            key: key,
            value: value
        })
    };
    /*从本地缓存拿数据*/
    coreUtil.getData = function (key) {
        var localData = layui.data('LocalData');
        return localData[key];
    };

    //判断字符是否为空的方法
    coreUtil.isEmpty = function (obj) {
        if (typeof obj == "undefined" || obj == null || obj == "") {
            return true;
        } else {
            return false;
        }
    }

    //字典数据回显
    coreUtil.selectDictLabel = function (datas, value) {
        datas = JSON.parse(datas);
        var label = "";
        $.each(datas, function (index, dict) {
            if (dict.value == ('' + value)) {
                label = dict.label;
                return false;
            }
        });
        //匹配不到，返回未知
        if (Coreutil.isEmpty(label)) {
            return "未知";
        }
        return label;
    }


    return coreUtil;
})(Coreutil, window);
